package com.insurance.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.insurance.common.enums.DeleteFlagEnum;
import com.insurance.dao.PartnerRolePermissionDao;
import com.insurance.entity.PartnerRolePermissionEntity;
import com.insurance.service.PartnerRolePermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.Query;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service("partnerRolePermissionService")
public class PartnerRolePermissionServiceImpl extends ServiceImpl<PartnerRolePermissionDao, PartnerRolePermissionEntity> implements PartnerRolePermissionService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PartnerRolePermissionEntity> page = this.page(
                new Query<PartnerRolePermissionEntity>().getPage(params),
                new QueryWrapper<PartnerRolePermissionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取查询对象
     *
     * @return
     */
    public QueryWrapper<PartnerRolePermissionEntity> getQueryWrapper(){
        QueryWrapper<PartnerRolePermissionEntity> queryWrapper = new QueryWrapper<PartnerRolePermissionEntity>();
        return queryWrapper;
    }

    /**
     * 通过角色主键查询角色权限对象集合
     *
     * @param roleId 角色主键
     * @return
     */
    @Override
    public List<PartnerRolePermissionEntity> getRolePermissionByRoleId(String roleId){
        if(StringUtils.isBlank(roleId)){
            return new ArrayList<PartnerRolePermissionEntity>();
        }
        QueryWrapper<PartnerRolePermissionEntity> queryWrapper = getQueryWrapper();
        queryWrapper.eq("role_id", roleId);
        return this.list(queryWrapper);
    }

    /**
     * 通过角色主键查询权限字符串集合
     *
     * @param roleId 角色主键
     * @return
     */
    @Override
    public List<String> getRolePermissionStrByRoleId(String roleId){
        List<PartnerRolePermissionEntity> rolePermissionEntityList = getRolePermissionByRoleId(roleId);
        if(CollectionUtil.isEmpty(rolePermissionEntityList)){
            return new ArrayList<String>();
        }
        List<String> permissionIdList = rolePermissionEntityList.stream().map(PartnerRolePermissionEntity::getPermissionId).collect(Collectors.toList());
        return permissionIdList;
    }

    /**
     * 通过角色主键集合查询权限字符串集合
     *
     * @param roleIdList 角色主键集合
     * @return
     */
    @Override
    public List<String> getRolePermissionStrByRoleIdList(List<String> roleIdList){
        if(CollectionUtil.isEmpty(roleIdList)){
            return new ArrayList<String>();
        }
        QueryWrapper<PartnerRolePermissionEntity> queryWrapper = getQueryWrapper();
        queryWrapper.in("role_id", roleIdList);
        List<PartnerRolePermissionEntity> partnerRolePermissionEntityList = this.list(queryWrapper);
        if(CollectionUtil.isEmpty(partnerRolePermissionEntityList)){
            return new ArrayList<String>();
        }
        List<String> permissionList = new ArrayList<String>();
        for (int i = 0; i < partnerRolePermissionEntityList.size(); i++) {
            String permissionId = partnerRolePermissionEntityList.get(i).getPermissionId();
            if(!permissionList.contains(permissionId)){
                permissionList.add(permissionId);
            }
        }
        return permissionList;
    }



    /**
     * 批量保存角色权限关系表
     *
     * @param rolePermissionList 角色权限集合
     * @return
     */
    @Override
    public boolean saveRolePermissionList(List<PartnerRolePermissionEntity> rolePermissionList){
        if(CollectionUtil.isEmpty(rolePermissionList)){
            return false;
        }
        return this.saveBatch(rolePermissionList);
    }

    @Override
    public boolean deleteRolePermissionByRoleId(String roleId){
        if(StringUtils.isBlank(roleId)){
            return true;
        }
        QueryWrapper<PartnerRolePermissionEntity> queryWrapper = getQueryWrapper();
        queryWrapper.eq("role_id", roleId);
        List<PartnerRolePermissionEntity> list = this.list(queryWrapper);
        if(CollectionUtil.isEmpty(list)){
            return true;
        }
        return this.remove(queryWrapper);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public boolean saveAndDelete(String roleId, List<PartnerRolePermissionEntity> rolePermissionList) throws Exception{
        boolean deleteFlag = this.deleteRolePermissionByRoleId(roleId);
        if(!deleteFlag){
            throw new Exception("保存角色权限中间表失败, 删除角色权限中间表失败");
        }
        boolean saveFlag = this.saveBatch(rolePermissionList);
        if(!saveFlag){
            throw new Exception("保存角色权限中间表失败, 保存角色权限中间表失败");
        }
        return true;
    }

}